#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# user_info_panel.py 
#
#  Copyright (c) 2006 INdT (Instituto Nokia de Technologia)
#
#  Author: Marcelo Lira dos Santos <setanta@gmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import pygtk
pygtk.require("2.0")
import gtk
import gobject
import tapioca
from debug import debug
import icon_theme
import util

from combo_presence import ComboPresence
from local_contact_store import LocalContactStore

class UserInfoPanel(gtk.HBox):

    (
        PANEL_NORMAL_VIEW,
        PANEL_SET_USER_ALIAS_VIEW,
        PANEL_CUSTOM_PRESENCE_VIEW
    ) = range(3)

    def __init__(self, parent, main_app, local_contact_store):
        gtk.HBox.__init__(self, spacing=6)
        
        self._parent = parent
        self.main_app = main_app
        self.local_contact_store = local_contact_store
        self.custom_presence_request = None
        self.user_alias = None
        self.panel_view = UserInfoPanel.PANEL_NORMAL_VIEW

        self._normal_cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)
        self._hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)

        self.presence_vbox = gtk.VBox(spacing=0)

        self.user_alias_label = gtk.Label("<b>Fulano</b>")
        self.user_alias_label.set_use_markup(True)
        self.user_alias_label.set_padding(3, 6)
        self.user_alias_label.set_alignment(0.0, 0.5)
        
        self.user_alias_event_box = gtk.EventBox()
        self.user_alias_event_box.add(self.user_alias_label)
        self.user_alias_event_box.connect('button-press-event', \
                self.on_user_alias_pressed)
        self.user_alias_event_box.connect('enter-notify-event', \
                self.on_user_alias_enter)
        self.user_alias_event_box.connect('leave-notify-event', \
                self.on_user_alias_leave)
        
        # Remember to pass the my_id
        self.combo_presence = ComboPresence(self.main_app, self.local_contact_store, '')
        self.combo_presence.connect('sign-out', \
                self.main_app.sign_out)
        self.combo_presence.connect('change-presence', \
                self.main_app.on_change_presence)
        self.combo_presence.connect('custom-presence', \
                self.on_custom_presence_request)
        
        self.custom_presence_hbox = gtk.HBox(spacing=6)

        self.presence_icon = gtk.Image()
        self.presence_icon.set_from_pixbuf( \
                self.local_contact_store.get_presence_icon_from_presence( \
                tapioca.PRESENCE_BUSY)
        )

        self.user_info_entry = gtk.Entry()
        self.user_info_entry.connect('activate', \
                self.on_user_info_entry_activate)
        self.user_info_entry.connect('key-press-event', \
                self.on_user_info_entry_key_press)
        self.user_info_entry.connect('focus-out-event', \
                self.on_user_entry_out)
        
        self.custom_presence_hbox.pack_start(self.presence_icon, \
                expand=False, fill=True)
        self.custom_presence_hbox.pack_start(self.user_info_entry, \
                expand=True, fill=True)

        self.custom_presence_hbox.set_no_show_all(True)
        
        self.custom_presence_align = gtk.Alignment(0.5, 0.5, 1, 1)
        self.custom_presence_align.set_padding(4, 0, 0, 0)
        self.custom_presence_align.add(self.custom_presence_hbox)
          
        self.presence_vbox.pack_start(self.user_alias_event_box, \
                expand=False, fill=False)
        self.presence_vbox.pack_start(self.combo_presence, \
                expand=True, fill=True)
        self.presence_vbox.pack_start(self.custom_presence_align, \
                expand=True, fill=True)

        self.avatar_icon = gtk.Viewport()
        self.avatar_icon.set_shadow_type(gtk.SHADOW_ETCHED_IN)

        pixbuf_avatar = self.local_contact_store.get_avatar_thumbnail(None)
        self.image_avatar = gtk.Image()
        self.image_avatar.set_from_pixbuf(pixbuf_avatar)

        self.avatar_icon.add(self.image_avatar)
        self.avatar_icon.connect('button-press-event', \
                self.on_avatar_icon_pressed)
        self.avatar_icon.connect('enter-notify-event', \
                self.on_avatar_icon_enter)
        self.avatar_icon.connect('leave-notify-event', \
                self.on_avatar_icon_leave)

        self.pack_start(self.presence_vbox, expand=True, fill=True)
        self.pack_start(self.avatar_icon, expand=False, fill=True)


    def set_id(self, id):
        self.combo_presence.set_id(id)

    def set_user_alias(self, alias):
        self.user_alias = alias
        self.user_alias_label.set_markup('<b>%s</b>' % alias)

    def set_user_avatar(self, avatar):
        pixbuf = util.pixbuf_from_avatar(avatar)
        pixbuf = util.rescale_pixbuf(pixbuf, 56.0)

        if pixbuf != None:
            self.image_avatar.set_from_pixbuf(pixbuf)

    def set_custom_presence_view(self, presence):
        self.panel_view = UserInfoPanel.PANEL_CUSTOM_PRESENCE_VIEW
        self.combo_presence.hide()
        self.custom_presence_hbox.show()
        self.presence_icon.show()

        if presence != None:
            self.presence_icon.set_from_pixbuf(self.local_contact_store.\
                    get_presence_icon_from_presence(presence))

        self.user_info_entry.show()
        self.user_info_entry.grab_focus()

    def set_normal_view(self):
        self.panel_view = UserInfoPanel.PANEL_NORMAL_VIEW
        self.custom_presence_hbox.hide()
        self.user_info_entry.set_text('')
        self.combo_presence.show()

        if self.user_alias != None:
            self.set_user_alias(self.user_alias)

    def set_user_alias_view(self):
        self.panel_view = UserInfoPanel.PANEL_SET_USER_ALIAS_VIEW
        self.user_alias_label.set_markup('<b>New Alias:</b>')
        self.combo_presence.hide()
        self.custom_presence_hbox.show()
        self.presence_icon.hide()
        self.user_info_entry.show()
        self.user_info_entry.grab_focus()

    def on_custom_presence_request(self, widget, presence):
        self.custom_presence_request = presence
        self.set_custom_presence_view(presence)

    def on_user_entry_out(self, widget, event):
        if self.panel_view != UserInfoPanel.PANEL_NORMAL_VIEW:
            self.combo_presence.set_active(\
                    self.combo_presence.last_active_item)
            self.set_normal_view()
    
    def on_user_info_entry_activate(self, widget):
        text = widget.get_text()

        if text.strip() == '':
            self.set_normal_view()
            return

        if self.panel_view == UserInfoPanel.PANEL_SET_USER_ALIAS_VIEW:
            self.user_alias = text
            self.main_app.set_alias(text)
        elif self.panel_view == UserInfoPanel.PANEL_CUSTOM_PRESENCE_VIEW:
            iter = self.combo_presence.insert_custom_presence(\
                    self.custom_presence_request, text)
            self.combo_presence.set_active_iter(iter)
            
        self.set_normal_view()
    
    def on_user_info_entry_key_press(self, widget, event):
        if event.keyval == 65307: # ESC key
            self.combo_presence.set_active(\
                    self.combo_presence.last_active_item)
            self.set_normal_view()

    def on_avatar_icon_pressed(self, widget, event):
        if event.button == 1: # left mouse button
            self._parent.show_select_avatar_window()

    def on_avatar_icon_enter(self, widget, event):
        print 'avatar icon enter'
        self._set_normal_cursor(False)

    def on_avatar_icon_leave(self, widget, event):
        self._set_normal_cursor()

    def on_user_alias_pressed(self, widget, event):
        if self.panel_view == UserInfoPanel.PANEL_NORMAL_VIEW and \
                event.button == 1: # left mouse button
            self._set_normal_cursor()
            self.user_info_entry.set_text(self.user_alias_label.get_text())
            self.set_user_alias_view()

    def on_user_alias_enter(self, widget, event):
        show_hand = (self.panel_view != UserInfoPanel.PANEL_NORMAL_VIEW)
        self._set_normal_cursor(show_hand)

    def on_user_alias_leave(self, widget, event):
        if self.panel_view != UserInfoPanel.PANEL_NORMAL_VIEW:
            return

        self._set_normal_cursor()

    def _set_normal_cursor(self, normal=True):
        gdk_window = self.get_display().get_window_at_pointer()

        if gdk_window == None:
            return
            
        if normal:
            cursor = self._normal_cursor
        else:
            cursor = self._hand_cursor

        gdk_window[0].set_cursor(cursor)

    def hide(self):
        self.combo_presence.save_custom_presences()
        self.combo_presence.clear_custom_presences()
        gtk.HBox.hide(self)

    def show(self):
        self.combo_presence.load_custom_presences()
        gtk.HBox.show(self)

